home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-22 | 8.6 KB | 154 lines | [TEXT/EXTE] |
- ** NeuraLab™ -- Neural Network Library
- ** Copyright © 1994-95 by Mikuni Berkeley R&D Corp. All right reserved.
-
- ** Version 1.0 -- May 15, 1994; by Song-Min Wang
- ** 1.5 -- 1/15/95 (SM Wang)
-
- constant NN_PASS_ARRAY_DIMENSION is 8;
- constant INFO_DIMENSION is 10;
-
- double _connArray[], _passedRealArray[];
- long _sizeArray[], _passedLongArray[],
- _dofOfPassedArray, _setOfPassedArray;
-
-
-
- // ========================================= Calculate Sigmoid function or its derivative
- double Sigmoid(double xx, double TT, integer fd)
- {
- double fSigm;
-
- fSigm = 1.0/(1.0+exp(-xx/TT));
-
- if (fSigm < 0.01) fSigm = 0.01;
- else if (fSigm > 0.99) fSigm = 0.99;
-
- if (fd)
- return fSigm*(1.0-fSigm)/TT; // Return the derivative when fd != 0
- else
- return fSigm; // Return function value when fd == 0
- }
-
- // ====================================== Calculate arctangent function or its derivative
- double Hypertan(double xx, double TT, double fd)
- {
- double f1, nnResult;
-
- f1 = exp(xx/TT);
- nnResult = (f1-1.0/f1)/(f1+1.0/f1);
-
- if (nnResult < -0.99) nnResult = -0.99;
- else if (nnResult > 0.99) nnResult = 0.99;
-
- if (fd)
- return (1.0-nnResult*nnResult)/TT;
- else
- return nnResult;
- }
-
- // =========================================== Calculate function value of Gauss function
- double Gauss(double xx, double Devi, integer fd)
- {
- double fG, pp;
-
- pp = xx/Devi;
- fG = exp(-0.5*pp*pp)/(Devi*Sqrt(2.0*PI));
- if (fd)
- return -pp*fG/Devi;
- else
- return fG;
- }
-
- // =================================================================== Minor error occurs
- procedure ErrorMsg(string msg0)
- {
- integer bNum;
-
- bNum = MyBlockNumber();
- if (msg0 == "")
- UserError("Error in [" + BlockName(bNum) + "], BN: " + bNum + ".");
- else
- UserError(msg0 + " Error in [" + BlockName(bNum) + "], BN: " + bNum + ".");
- }
-
- // ================================================================= Serious error occurs
- procedure ErrorAbort(string msg0)
- {
- ErrorMsg(msg0);
- AbortAllSims();
- }
-
- // ========================================================= Check for positive long data
- ** upperBound < 1.0 means no upper bound
- double ValueValidation(double xx, double upperBound)
- {
- if (NoValue(xx) || xx < 1.0)
- ErrorAbort("Invalid data setting: "+xx);
- else if (upperBound > 1.0 && xx > upperBound)
- ErrorAbort("The input data exceed the upper limit.");
- else
- return Floor(xx+0.5);
- }
-
- // ======================================================================================
- procedure SetMyBlockLabel(Str255 lab)
- {
- long nB;
-
- nB = MyBlockNumber();
- SetBlockLabel(nB, lab);
- }
-
- //******************************** Functions that handle passing array by block connector
- // ============================== Extract information and array pointer from passed array
- // List of return values: 0 -- No error.\
- // 1 -- The connector value is not a array
- // -1 -- This is a regular array
- // 2 -- Wrong array dimension
-
- // (Error codes 4, 8, 16, 32 can be combined)
- // 4 -- Can't get size information of the real array
- // 8 -- Wrong dimension of the size array
- // 16 -- Can't get the passed real array
- // 32 -- Array dimension and the size information are incompatible
- //
- double ExtractRealArrayOld(double passed)
- {
- double nnResult;
- long ll, ii;
-
- nnResult = 0.0;
- if (!GetPassedArray(passed, _connArray))
- return 1.0; //The passed data is not an array
- if (!NoValue(_connArray[0]))
- return -1.0;; //This is a regular array
- if (GetDimension(_connArray) != 8)
- return 2.0;
-
- if (!GetPassedArray(_connArray[2], _sizeArray))
- nnResult += 4.0;
- if (GetDimension(_sizeArray) != _connArray[1]+1)
- nnResult += 8.0;
-
- if (!GetPassedArray(_connArray[3], _passedRealArray))
- nnResult += 16.0;
- ll = 1;
- for (ii=0; ii<=_connArray[1]; ii++)
- ll *= _sizeArray[ii];
- if (GetDimension(_passedRealArray) != ll) {
- nnResult += 32.0;
- UserError("ll="+ll+" "+GetDimension(_passedRealArray));
- }
-
- if (nnResult == 0.0)
- return _connArray[3];
- else
- return nnResult;
-
-
- }
-
- // =================================================================================
- procedure CheckNNPassedArray(double passed)
- {
- if (!GetPassedArray(passed, _connArray))
- ErrorAbort("Can't get passed array.");
-
- if (!NoValue(_connArray[0]))
- ErrorAbort("The passed array does not have a proper format.");
-
- if (GetDimension(_connArray) != NN_PASS_ARRAY_DIMENSION)
- ErrorAbort("The passed array does not has a right dimension.");
- }
-
- // =================================================================================
- long ExtractArrayDOF(double passed)
- {
- long nnResult;
-
- GetPassedArray(passed, _connArray);
-
- nnResult = Floor(_connArray[1]+0.5);
- if (nnResult < 1)
- ErrorAbort("Invalid DOF of the passed arrays.");
- else
- return nnResult;
-
-
- }
-
- // =================================================================================
- long ExtractSetNumber(double passed)
- {
- long nnResult;
-
- GetPassedArray(passed, _connArray);
-
-
- nnResult = Floor(_connArray[2]+0.5);
- if (nnResult < 1)
- ErrorAbort("Invalid DOF of the passed arrays.");
- else
- return nnResult;
-
-
-
- }
-
- // =================================================================================
- long ExtractRowNumber(double passed, long setIndex)
- {
- long nnDOF, nnSet;
-
- GetPassedArray(passed, _connArray);
-
- if (setIndex < 0)
- setIndex = 0;
- nnDOF = ExtractArrayDOF(passed);
-
- nnSet = ExtractSetNumber(passed);
- if (setIndex > nnSet-1)
- ErrorAbort("The passed array has a invalid number of sets.");
- else if (nnSet == 1 && nnDOF == 1)
- return _connarray[3];
- else {
- GetPassedArray(_connArray[3], _sizeArray);
- return _sizeArray[setIndex*nnDOF];
-
- }
- }
-
- // =================================================================================
- long ExtractColNumber(double passed, long setIndex)
- {
- long nnDOF, nnSet;
-
- GetPassedArray(passed, _connArray);
-
-
- if (setIndex < 0)
- setIndex = 0;
- nnDOF = ExtractArrayDOF(passed);
- if (nnDOF == 1)
- return 1;
-
- nnSet = ExtractSetNumber(passed);
- if (setIndex > nnSet-1)
- ErrorAbort("The passed array has a invalid number of sets.");
- else {
- GetPassedArray(_connArray[3], _sizeArray);
- return _sizeArray[setIndex*nnDOF+1];
-
- }
- }
-
- // =================================================================================
- double ExtractSizeArray(double passed)
- {
- GetPassedArray(passed, _connArray);
- return _connArray[3];
- }
-
- // =================================================================================
- ** setIndex = N : The N-th passed array (the index starts from 0)
- **
- double ExtractRealArray(double passed)
- {
- double nnResult;
- long ll, ll1, ii, jj;
-
-
- nnResult = 0.0;
- GetPassedArray(passed, _connArray);
-
- _dofOfPassedArray = Floor(_connArray[1]+0.5);
- if (_dofOfPassedArray < 1)
- ErrorAbort("The passed array has an invalid DOF.");
-
- _setOfPassedArray = Floor(_connArray[2]+0.5);
- if (_setOfPassedArray < 1)
- ErrorAbort("The passed array has a unknown number of sets.");
-
- if (_setOfPassedArray == 1 && _dofOfPassedArray == 1) {
- GetPassedArray(_connarray[4], _passedRealArray);
- if (GetDimension(_passedRealArray) == _connArray[3])
- return _connarray[4];
- else
- ErrorAbort("The passed array does not match its own dimension.");
- }
- else {
- GetPassedArray(_connArray[4], _passedRealArray);
- GetPassedArray(_connArray[3], _sizeArray);
- ll = 0;
- for (ii=0; ii<_connArray[2]; ii++) {
- ll1 = 1;
- for (jj=0; jj<_connArray[1]; jj++)
- ll1 *= _sizeArray[ii*_connArray[1]+jj];
- ll += ll1;
- }
- if (GetDimension(_passedRealArray) == ll)
- return _connArray[4];
- else
- ErrorAbort("The passed arrays do not match their own dimensions (" +
- ll + " vs " + GetDimension(_passedRealArray) + ").");
- }
-
-
- }
-
- // ----------------------------------------------------------------------------------
- double ExtractInfo1Array(double passed)
- {
- CheckNNPassedArray(passed);
-
-
- return _connArray[5];
-
-
- }
-
- // ==================================================================================
- double ExtractLongArray(double passed)
- {
- double nnResult;
-
- nnResult = 0.0;
- if (!GetPassedArray(passed, _connArray))
- return 1.0; //Can't get passed array
- if (!NoValue(_connArray[0]))
- return -1.0;; //This is a regular array
- if (_connArray[1] != 1 || GetDimension(_connArray) != 8)
- return 2.0;
-
- if (!GetPassedArray(_connArray[2], _sizeArray))
- nnResult += 4.0;
- if (GetDimension(_sizeArray) != 2)
- nnResult += 8.0;
-
- if (!GetPassedArray(_connArray[3], _passedLongArray))
- nnResult += 16.0;
- if (GetDimension(_passedLongArray) != _sizeArray[0]*_sizeArray[1])
- nnResult += 32.0;
-
- if (nnResult == 0.0)
- return _connArray[3];
- else
- return nnResult;
-
-
- }
-